﻿using System.Text;
using System.IO;
using System.Reflection;

namespace comm
{
    public static class STRes
    {
        public static Stream GetStream(Assembly assembly, string resName)
        {
            return GetStream(assembly, resName, false);
        }
        public static Stream GetStream(Assembly assembly, string resName, bool fullResName)
        {
            if (fullResName)
            {
                return assembly.GetManifestResourceStream(resName);
            }
            var ss = assembly.FullName.Split(',');
            var name = ss[0] + "." + resName;
            if (resName.IndexOf(ss[0]) == 0)
            {
                name = resName;
            }
            return assembly.GetManifestResourceStream(name);
        }
        public static byte[] GetBytes(Assembly assembly, string resName)
        {
            return GetBytes(assembly, resName, false);
        }
        public static byte[] GetBytes(Assembly assembly, string resName, bool fullResName)
        {
            using (var st = GetStream(assembly, resName, fullResName))
            {
                var bytes = new byte[st.Length];
                st.Read(bytes, 0, bytes.Length);
                return bytes;
            }
        }
        public static string GetString(Assembly assembly, string resName)
        {
            return GetString(assembly, resName, false);
        }
        public static string GetString(Assembly assembly, string resName, bool fullResName)
        {
            using (var st = GetStream(assembly, resName, fullResName))
            {
                using (var sr = new StreamReader(st))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }
}
